﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_InsertCompetition : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        CompetitionInfo competInfo = new CompetitionInfo();
        CompetitionDAO competDAO = new CompetitionDAO();
        AwardDAO awaDAO = new AwardDAO();
        try
        {
            bool insert = true;
            competInfo.CompetitionName = txtCompetitionName.Text;
            competInfo.Details = txtDetails.Text;
            competInfo.Conditions = txtCondition.Text;
            competInfo.StartDate = DateTime.Parse(txtStartdate.Text);
            competInfo.EndDate = DateTime.Parse(txtEndDate.Text);

            if (competInfo.EndDate < competInfo.StartDate)
            {
             
                insert = false;
 
            } else if (fuPainting.HasFile)
            {
              
                    int intFileSizeLimit = 2000;
                    string postedFile = fuPainting.PostedFile.FileName;
                    string extension = System.IO.Path.GetExtension(postedFile);
                    string fileName = System.IO.Path.GetFileName(postedFile);
                    int size = fuPainting.PostedFile.ContentLength / 1024;
                    extension = extension.ToLower();

                    // if (fuPainting.PostedFile.ContentType == "image/jpeg")
                    if (extension.Equals(".jpg") || extension.Equals(".gif"))
                    {
                        if (size < intFileSizeLimit)
                        {
                            competInfo.Url = ("Images/") + fuPainting.FileName.ToString();
                            fuPainting.PostedFile.SaveAs(Server.MapPath("~/Images/") + fileName);
                            lblMessage.ForeColor = System.Drawing.Color.White;
                            lblMessage.Text = "<br /><br />" + "Uploaded Successful file details <hr />" +
                                "File Name: " + fileName + "<br />" +
                                "File Extension Name: " + extension + "<br />" +
                                "File Size: " + size.ToString() + "KB";
                        }
                        else
                        {

                            lblMessage.Text = "<br /><br /> <hr />  File size exceeded than limit " + intFileSizeLimit + " KB, Please upload smaller file.";
                        }
                    }


                    else
                    {
                        lblMessage.ForeColor = System.Drawing.Color.Red;
                        lblMessage.Text = " <br /><br /> <hr /> Only image/jpeg file are allowed, try again!";

                    }





                }
               // catch (Exception ex)
             //   {
              //      lblMessage.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
              //  }
          //  }
            if (insert == true)
            {
                if (System.IO.Path.GetExtension(fuPainting.PostedFile.FileName).Equals(".jpg") || System.IO.Path.GetExtension(fuPainting.PostedFile.FileName).Equals(".gif"))
                    if (competDAO.InsertCompetition(competInfo) > 0)
                    {
                        AwardInfo award1 = new AwardInfo();
                        AwardInfo award2 = new AwardInfo();
                        AwardInfo award3 = new AwardInfo();

                        award1.AwardName = "1st";
                        award2.AwardName = "2nd";
                        award3.AwardName = "3rd";

                        award1.Prize = txtAward_1st.Text;
                        award2.Prize = txtAward_2nd.Text;
                        award3.Prize = txtAward_3rd.Text;

                        award1.CompetitionId = competInfo.CompetitionID;
                        award2.CompetitionId = competInfo.CompetitionID;
                        award3.CompetitionId = competInfo.CompetitionID;

                        awaDAO.InsertAward(award1);
                        awaDAO.InsertAward(award2);
                        awaDAO.InsertAward(award3);
                        Reset();
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Insert Successfull!');", true);
                    }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "redirectMe", "alert('Insert Failed!');", true);
                lblMessage.Text = "<br /><br /> <hr /> Insert Failed! The EndDate must be greater than StartDate ";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
           

        }
        catch (Exception ex) { lblMessage.Text = "Insert Failed!!! + Because: +"+ex.Message; }
        
    }
    protected void btnSeset_Click(object sender, EventArgs e)
    {
        Reset();

    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("MaintainCompetitions.aspx");
    }

    void Reset()
    {
        txtCompetitionName.Text = "";
        txtCondition.Text = "";
        txtDetails.Text = "";
        txtEndDate.Text = "";
        txtStartdate.Text = "";
        txtAward_3rd.Text = "";
        txtAward_2nd.Text = "";
        txtAward_1st.Text = "";
        lblMessage.Text = "";
    }
}